const connection = require('../app/database')

class UserService {
  // 插入数据到数据库
  async create(user) {

    // 1.获取请求参数
    const { name, password } = user

    // 2.定义预处理语句
    const statement = 'INSERT INTO `user` ( `name`, `password`) VALUES (?, ?);'

    // 3.执行sql语句
    let [res, fields] = await connection.execute(statement, [name, password])
    return res
  }

  // 查找数据
  async findUserByName(name) {
    const statement = 'SELECT* FROM `user` WHERE `name` = ?;'

    let [res, fields] = await connection.execute(statement, [name])
    return res
  }

  // 更新avatar_url数据
  async updateUserAvatarUrl(avatar_url,user_id) {
    const statement = 'UPDATE user SET  avatar_url = ? WHERE id =?;'

    let [res, fields] = await connection.execute(statement, [avatar_url,user_id])
    return res
  }
}

module.exports = new UserService()